//------------------------------------------------------------------------------
// The contents of this file are subject to the nopCommerce Public License Version 1.0 ("License"); you may not use this file except in compliance with the License.
// You may obtain a copy of the License at  http://www.nopCommerce.com/License.aspx. 
// 
// Software distributed under the License is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. 
// See the License for the specific language governing rights and limitations under the License.
// 
// The Original Code is nopCommerce.
// The Initial Developer of the Original Code is NopSolutions.
// All Rights Reserved.
// 
// Contributor(s): _______. 
//------------------------------------------------------------------------------

using System;
using System.Collections.Generic;
using System.Text;
using Tathata.Common.Caching;
using Microsoft.Practices.EnterpriseLibrary.Data;
using System.Data.Common;
using System.Data;
using System.Globalization;
using Tathata.Common.DAL;
using Tathata.Common.BLL.Entities;
using Tathata.Common.BLL.EntityCollections;

namespace Tathata.Common.DAL.Managers
{
    internal class WarehouseDBManager
    {
        #region Methods
        public static WarehouseCollection GetAllWarehouses()
        {
            WarehouseCollection warehouseCollection = new WarehouseCollection();
            Database db = NopDataHelper.CreateConnection();
            DbCommand dbCommand = db.GetStoredProcCommand("Nop_WarehouseLoadAll");
            using (IDataReader dataReader = db.ExecuteReader(dbCommand))
            {
                while (dataReader.Read())
                {
                    Warehouse warehouse = new Warehouse();
                    warehouse.WarehouseID = NopDataHelper.GetInt(dataReader, "WarehouseID");
                    warehouse.Name = NopDataHelper.GetString(dataReader, "Name");
                    warehouse.PhoneNumber = NopDataHelper.GetString(dataReader, "PhoneNumber");
                    warehouse.Email = NopDataHelper.GetString(dataReader, "Email");
                    warehouse.FaxNumber = NopDataHelper.GetString(dataReader, "FaxNumber");
                    warehouse.Address1 = NopDataHelper.GetString(dataReader, "Address1");
                    warehouse.Address2 = NopDataHelper.GetString(dataReader, "Address2");
                    warehouse.City = NopDataHelper.GetString(dataReader, "City");
                    warehouse.StateProvince = NopDataHelper.GetString(dataReader, "StateProvince");
                    warehouse.ZipPostalCode = NopDataHelper.GetString(dataReader, "ZipPostalCode");
                    warehouse.CountryID = NopDataHelper.GetInt(dataReader, "CountryID");
                    warehouse.Deleted = NopDataHelper.GetBoolean(dataReader, "Deleted");
                    warehouse.CreatedOn = NopDataHelper.GetDateTime(dataReader, "CreatedOn");
                    warehouse.UpdatedOn = NopDataHelper.GetDateTime(dataReader, "UpdatedOn");
                    warehouseCollection.Add(warehouse);
                }
            }

            return warehouseCollection;
        }

        public static Warehouse GetByWarehouseID(int WarehouseID)
        {
            Warehouse warehouse = null;
            Database db = NopDataHelper.CreateConnection();
            DbCommand dbCommand = db.GetStoredProcCommand("Nop_WarehouseLoadByPrimaryKey");
            db.AddInParameter(dbCommand, "WarehouseID", DbType.Int32, WarehouseID);
            using (IDataReader dataReader = db.ExecuteReader(dbCommand))
            {
                if (dataReader.Read())
                {
                    warehouse = new Warehouse();
                    warehouse.WarehouseID = NopDataHelper.GetInt(dataReader, "WarehouseID");
                    warehouse.Name = NopDataHelper.GetString(dataReader, "Name");
                    warehouse.PhoneNumber = NopDataHelper.GetString(dataReader, "PhoneNumber");
                    warehouse.Email = NopDataHelper.GetString(dataReader, "Email");
                    warehouse.FaxNumber = NopDataHelper.GetString(dataReader, "FaxNumber");
                    warehouse.Address1 = NopDataHelper.GetString(dataReader, "Address1");
                    warehouse.Address2 = NopDataHelper.GetString(dataReader, "Address2");
                    warehouse.City = NopDataHelper.GetString(dataReader, "City");
                    warehouse.StateProvince = NopDataHelper.GetString(dataReader, "StateProvince");
                    warehouse.ZipPostalCode = NopDataHelper.GetString(dataReader, "ZipPostalCode");
                    warehouse.CountryID = NopDataHelper.GetInt(dataReader, "CountryID");
                    warehouse.Deleted = NopDataHelper.GetBoolean(dataReader, "Deleted");
                    warehouse.CreatedOn = NopDataHelper.GetDateTime(dataReader, "CreatedOn");
                    warehouse.UpdatedOn = NopDataHelper.GetDateTime(dataReader, "UpdatedOn");
                }
            }
            return warehouse;
        }

        public static Warehouse InsertWarehouse(string Name, string PhoneNumber, string Email, string FaxNumber,
            string Address1, string Address2, string City, string StateProvince,
            string ZipPostalCode, int CountryID, bool Deleted, DateTime CreatedOn, DateTime UpdatedOn)
        {
            Warehouse warehouse = null;
            Database db = NopDataHelper.CreateConnection();
            DbCommand dbCommand = db.GetStoredProcCommand("Nop_WarehouseInsert");
            db.AddOutParameter(dbCommand, "WarehouseID", DbType.Int32, 0);
            db.AddInParameter(dbCommand, "Name", DbType.String, Name);
            db.AddInParameter(dbCommand, "PhoneNumber", DbType.String, PhoneNumber);
            db.AddInParameter(dbCommand, "Email", DbType.String, Email);
            db.AddInParameter(dbCommand, "FaxNumber", DbType.String, FaxNumber);
            db.AddInParameter(dbCommand, "Address1", DbType.String, Address1);
            db.AddInParameter(dbCommand, "Address2", DbType.String, Address2);
            db.AddInParameter(dbCommand, "City", DbType.String, City);
            db.AddInParameter(dbCommand, "StateProvince", DbType.String, StateProvince);
            db.AddInParameter(dbCommand, "ZipPostalCode", DbType.String, ZipPostalCode);
            db.AddInParameter(dbCommand, "CountryID", DbType.Int32, CountryID);
            db.AddInParameter(dbCommand, "Deleted", DbType.Boolean, Deleted);
            db.AddInParameter(dbCommand, "CreatedOn", DbType.DateTime, CreatedOn);
            db.AddInParameter(dbCommand, "UpdatedOn", DbType.DateTime, UpdatedOn);
            if (db.ExecuteNonQuery(dbCommand) > 0)
            {
                int WarehouseID = Convert.ToInt32(db.GetParameterValue(dbCommand, "@WarehouseID"));
                warehouse = GetByWarehouseID(WarehouseID);
            }

            return warehouse;
        }

        public static Warehouse UpdateWarehouse(int WarehouseID, string Name, string PhoneNumber, string Email, string FaxNumber,
            string Address1, string Address2, string City, string StateProvince,
            string ZipPostalCode, int CountryID, bool Deleted, DateTime CreatedOn, DateTime UpdatedOn)
        {
            Warehouse warehouse = null;
            Database db = NopDataHelper.CreateConnection();
            DbCommand dbCommand = db.GetStoredProcCommand("Nop_WarehouseUpdate");
            db.AddInParameter(dbCommand, "WarehouseID", DbType.Int32, WarehouseID);
            db.AddInParameter(dbCommand, "Name", DbType.String, Name);
            db.AddInParameter(dbCommand, "PhoneNumber", DbType.String, PhoneNumber);
            db.AddInParameter(dbCommand, "Email", DbType.String, Email);
            db.AddInParameter(dbCommand, "FaxNumber", DbType.String, FaxNumber);
            db.AddInParameter(dbCommand, "Address1", DbType.String, Address1);
            db.AddInParameter(dbCommand, "Address2", DbType.String, Address2);
            db.AddInParameter(dbCommand, "City", DbType.String, City);
            db.AddInParameter(dbCommand, "StateProvince", DbType.String, StateProvince);
            db.AddInParameter(dbCommand, "ZipPostalCode", DbType.String, ZipPostalCode);
            db.AddInParameter(dbCommand, "CountryID", DbType.Int32, CountryID);
            db.AddInParameter(dbCommand, "Deleted", DbType.Boolean, Deleted);
            db.AddInParameter(dbCommand, "CreatedOn", DbType.DateTime, CreatedOn);
            db.AddInParameter(dbCommand, "UpdatedOn", DbType.DateTime, UpdatedOn);
            if (db.ExecuteNonQuery(dbCommand) > 0)
                warehouse = GetByWarehouseID(WarehouseID);

            return warehouse;
        }
        #endregion
    }
}
